﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class SQLHelper
    {
        private SqlConnection conn = null;
        private SqlCommand cmd = null;
        private SqlDataReader sdr = null;
        public SQLHelper()
        {
            string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            conn = new SqlConnection(connStr);
        }
        #region  如果数据库连接关闭，将其打开
        /// <summary>
        /// 如果数据库连接关闭，将其打开
        /// </summary>
        /// <returns></returns>
        private SqlConnection GetConn()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            return conn;
        }
        #endregion
        #region 该方法执行不带参数的增、删、改SQL语句或存储过程
        /// <summary>
        /// 该方法执行不带参数的增、删、改SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">要执行的增、删、改SQL语句或存储过程名称</param>
        /// <param name="CT">CommandType的属性是SQL还是存储过程</param>
        /// <returns></returns>
        public int ExecuteNonQuery(string cmdText, CommandType CT)
        {
            int res;
            using (cmd = new SqlCommand(cmdText, GetConn()))
            {
                cmd.CommandType = CT;
                res = cmd.ExecuteNonQuery();
            }
            return res;

        }
        #endregion
        #region 该方法执行 带参数 的增、删、改SQL语句或存储过程
        /// <summary>
        /// 该方法执行 带参数 的增、删、改SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">要执行的增、删、改的SQL语句或存储过程名称</param>
        /// <param name="spa">参数数组</param>
        /// <param name="CT">CommandType的属性是SQL还是存储过程</param>
        /// <returns></returns>
        public int ExecuteNonQuery(string cmdText, SqlParameter[] spa, CommandType CT)
        {

            int res;
            using (cmd = new SqlCommand(cmdText, GetConn()))
            {
                cmd.Parameters.AddRange(spa);
                cmd.CommandType = CT;
                res = cmd.ExecuteNonQuery();
            }
            return res;

        }
        #endregion
        #region 该方法执行不带参数的查询的SQL语句或存储过程
        /// <summary>
        /// 该方法执行不带参数的查询的SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">要传入的SQL语句或存储过程名称</param>
        /// <param name="CT">CommandType的属性是SQL还是存储过程</param>
        /// <returns></returns>
        public DataTable ExecuteQuery(string cmdText, CommandType CT)
        {
            DataTable dt = new DataTable();
            cmd = new SqlCommand(cmdText, GetConn());
            cmd.CommandType = CT;
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;

        }
        #endregion
        #region 该方法执行带参数的SQl语句或存储过程
        /// <summary>
        /// 该方法执行带参数的SQl语句或存储过程
        /// </summary>
        /// <param name="cmdText">要执行的SQL语句或存储过程名称</param>
        /// <param name="spa">参数数组</param>
        /// <param name="CT">CommandType的属性是SQL还是存储过程</param>
        /// <returns></returns>
        public DataTable ExecuteQuery(string cmdText, SqlParameter[] spa, CommandType CT)
        {
            DataTable dt = new DataTable();
            cmd = new SqlCommand(cmdText, GetConn());
            cmd.Parameters.AddRange(spa);
            cmd.CommandType = CT;
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }
        #endregion
        #region DataSet该方法执行不带参数的查询的SQL语句或存储过程返回
        /// <summary>
        /// 该方法执行不带参数的查询的SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">要传入的SQL语句或存储过程名称</param>
        /// <param name="CT">CommandType的属性是SQL还是存储过程</param>
        /// <returns></returns>
        public DataSet ExecuteQueryDataSet(string cmdText, CommandType CT)
        {
          
            cmd = new SqlCommand(cmdText, GetConn());
            cmd.CommandType = CT;
            DataSet ds = new DataSet();
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {               
                da.Fill(ds);               
            }
            return ds;

        }
        #endregion
        #region DataSet该方法执行带参数的SQl语句或存储过程
        /// <summary>
        /// 该方法执行带参数的SQl语句或存储过程
        /// </summary>
        /// <param name="cmdText">要执行的SQL语句或存储过程名称</param>
        /// <param name="spa">参数数组</param>
        /// <param name="CT">CommandType的属性是SQL还是存储过程</param>
        /// <returns></returns>
        public DataSet ExecuteQueryDataSet(string cmdText, SqlParameter[] spa, CommandType CT)
        {
            DataSet ds = new DataSet();
            cmd = new SqlCommand(cmdText, GetConn());
            cmd.Parameters.AddRange(spa);
            cmd.CommandType = CT;
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(ds);
            }
            return ds;
        }
        #endregion


    }
}
